package assets_withdraw

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/emun"
	"gitee.com/liuxuezhan/ar-platform/base-common/tool/wlog"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"
	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
)

type FindWithDrawLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// c端财务管理页面--提现记录
func NewFindWithDrawLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FindWithDrawLogic {
	return &FindWithDrawLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *FindWithDrawLogic) FindWithDraw(req *types.FindWithDrawReq) (resp *types.FindWithDrawRes, err error) {
	rpc_req := &rpc.FindWithDrawReq{
		Page: &rpc.Page{
			Page:      req.Page.Page,
			PageSize:  req.Page.PageSize,
			SortOrder: req.Page.SortOrder,
		},
	}
	copier.Copy(rpc_req, req)

	// 发送请求
	rpc_resp, err := l.svcCtx.AssetsRpc.PayWithdraw.WithdrawListPage(l.ctx, rpc_req)
	if err != nil {
		wlog.Handle.Errorf(l.ctx, " %v", err)
		return nil, err
	}

	// 处理请求返回的结果
	// 解析、组合数据
	resp = &types.FindWithDrawRes{
		Data:       nil,
		Pagination: types.Pagination{},
	}
	if rpc_resp.Data != nil {
		for i, _ := range rpc_resp.Data {
			d := &types.WithDraw{}
			copier.Copy(d, rpc_resp.Data[i])
			if rpc_resp.Data[i].UserBank != nil {
				d.UserBank = &types.UserBankInfo{}
				copier.Copy(d.UserBank, rpc_resp.Data[i].UserBank)
			}
			if rpc_resp.Data[i].UserEwallets != nil {
				d.UserEwallets = &types.UserEWalletsInfo{}
				copier.Copy(d.UserEwallets, rpc_resp.Data[i].UserEwallets)
			}
			if rpc_resp.Data[i].UserLocalEwallets != nil {
				d.UserLocalEwallets = &types.UserLocalEwalletInfo{}
				copier.Copy(d.UserLocalEwallets, rpc_resp.Data[i].UserLocalEwallets)
			}
			if rpc_resp.Data[i].UserPixWallets != nil {
				d.UserPixWallets = &types.UserPixWalletsInfo{
					WalletTypeName: (emun.GetUserPixWalletNameByType(int(rpc_resp.Data[i].UserPixWallets.WalletType))),
				}
				copier.Copy(d.UserPixWallets, rpc_resp.Data[i].UserPixWallets)
			}
			if rpc_resp.Data[i].UserUsdtWallets != nil {
				d.UserUsdtWallets = &types.UserUsdtWalletsInfo{}
				copier.Copy(d.UserUsdtWallets, rpc_resp.Data[i].UserUsdtWallets)
			}
			resp.Data = append(resp.Data, d)
		}

	}
	resp.Pagination = types.Pagination{
		PageSize: int32(rpc_resp.Page.PageSize),
		Page:     int32(rpc_resp.Page.Page),
		Total:    rpc_resp.Page.Total,
	}
	return
}
